import QtQuick 1.0

Rectangle {
    property alias fileModel: fileListModelView.model
    color: root.constants.defaultBackgroundColor

    PathController {
        id: pathController
        width: parent.width
        height: root.constants.pathControllerHeight
        anchors.left: parent.left
        anchors.right: parent.right
        z: 1
    }

    ListView {
        id: fileListModelView
        width: parent.width
        height: pathController.opacity > 0 ?
                    parent.height - pathController.height :
                    parent.height
        anchors.top: pathController.bottom

        delegate: Rectangle {
            id: fileListModelItemDelegate
            width: parent.width
            height: root.constants.fileListModelItemHeight

            property string delegateColor:
                delegateMouseArea.pressed ?
                    root.constants.highlightColorAccent :
                    (status == 1) ?
                        root.constants.activeColorAccent :
                        (status == 3) ?
                            root.constants.activeColor :
                            root.constants.inactiveColorAccent;

            property string navigationArrowSource:
                (status == 1 || status == 4) ?
                    "arrow_right_green_icon.png" :
                    "arrow_right_icon.png";

            property bool marked: (status == 1);
            property bool backedup: (status == 3);

            gradient: Gradient {
                GradientStop { position: 0.0; color: root.constants.defaultBackgroundColor }
                GradientStop { position: 0.95; color: root.constants.defaultBackgroundColor }
                GradientStop { position: 1.0; color: delegateColor }
            }

            Row {
                anchors.fill: parent
                spacing: root.constants.defaultLayoutSpacing

                Item {
                    id: typeIcon
                    width: parent.width * 0.2
                    height: root.constants.fileListModelItemIconHeight
                    anchors.verticalCenter: parent.verticalCenter

                    Image {
                        anchors.fill: parent
                        opacity: (marked || backedup) ? 1.0 : 0.0;
                        fillMode: Image.PreserveAspectFit
                        smooth: true
                        source: (marked) ? "marked.png" : "backedup.png";
                    }

                    Image {
                        anchors.fill: parent
                        opacity: (marked || backedup) ? 0.8 : 1.0;
                        fillMode: Image.PreserveAspectFit
                        smooth: true
                        source: type ? "file_icon.png" : "folder_icon.png"
                    }
                }

                Text {
                    width: parent.width - typeIcon.width - navigationArrow.width
                    anchors.verticalCenter: parent.verticalCenter
                    verticalAlignment: Text.AlignVCenter
                    color: (fileListModelItemDelegate.delegateColor == root.constants.inactiveColorAccent) ?
                               root.constants.defaultForegroundColor :
                               fileListModelItemDelegate.delegateColor;
                    font.pixelSize: root.constants.defaultFontPixelSize
                    elide: Text.ElideRight
                    text: name
                }

                Image {
                    id: navigationArrow
                    opacity: type ? 0 :
                        navigationArrowMouseArea.pressed ?
                            1.0 : root.constants.unselectedIconOpacity
                    width: type ? 0 : parent.width * 0.3
                    height: root.constants.fileListModelItemIconHeight
                    anchors.verticalCenter: parent.verticalCenter
                    fillMode: Image.PreserveAspectFit
                    smooth: true
                    source: fileListModelItemDelegate.navigationArrowSource

                    MouseArea {
                        id: navigationArrowMouseArea
                        anchors.fill: parent

                        onClicked: {
                            haptics.buttonTapFeedback();
                            engine.navigateToSourceFolderByIndex(index);
                        }
                    }
                }
            }

            MouseArea {
                id: delegateMouseArea
                height: parent.height
                width: parent.width - navigationArrow.width

                anchors {
                    top: parent.top
                    bottom: parent.bottom
                    left: parent.left
                }

                onClicked: {
                    haptics.buttonTapFeedback();
                    engine.selectSourceFileModelIndex(index);
                }
            }
        } // delegate

        Connections {
            target: engine

            onSourcePathChanged: {
                console.debug("Source path changed, setting index to 0.");
                fileListModelView.currentIndex = 0;
            }
        }
    } // ListView
}
